import { _decorator, Animation, Camera, find, Game, math, Node, Quat, tween, UITransform, v3 } from 'cc';
import { NailBase } from './NailBase';
const { ccclass, property } = _decorator;

/**
 * 钉子
 */
@ccclass('DingZi')
export class DingZi extends NailBase {

    @property(Animation)
    anim: Animation = null

    private _toPoint: string = 'GamePlay/UI3D/NailCollectManager/nailCollect/points/point0'
    protected canPull(): void {
        // if (this.onCanPull) {
        //     this.onCanPull(this.node, this.itemID)
        // }
   
        console.log("可以拔出 uiPos_v3")

        const parent = find(this._toPoint)
        const parentPos = parent.worldPosition.clone()

        const selfWorldPos = this.node.worldPosition.clone()
        const selfWorldQuat = this.node.worldRotation.clone()
        parent.addChild(this.node)
        this.node.setWorldPosition(selfWorldPos)
        this.node.setWorldRotation(selfWorldQuat)

        const quat1 = math.quat()
        Quat.fromEuler(quat1, 60, 30, 0)

        const quat2 = math.quat()
        Quat.fromEuler(quat2, 90, 0, 0)
        

        tween(this.node)
            .to(
                0.5,
                {
                    worldPosition: v3(selfWorldPos.x, selfWorldPos.y, parentPos.z + 0.2),
                    worldRotation: quat1
                }
            )
            .to(
                2, 
                {
                    worldPosition: parentPos,
                    worldRotation: quat2

                }
            )
            .call(() => {
                if (this.onCanPull) {
                    this.onCanPull(this.node, this.itemID)
                }
            })
            .start()
    }

    protected cannotPull(block_node_: Node): void {
        super.cannotPull(block_node_)
     
        this.anim.play()
        console.log("不可以拔出")
    }
}